<?
class AdvCover extends Module
{
	function AdvCover($row)
	{
		Module::Module($row);
		$this->add_form(new AdvCoverForm());
	}
}

class AdvCoverForm extends Form 
{
	function AdvCoverForm()
	{
		Form::Form("AdvCoverForm");
	}
	
	function on_submit()
	{
		if(User::have_permit(ADMIN_ADV))
		{
			$status			= (int)Url::get('status',0);
			$url_detail		= Url::get('url');
			$type			= (int)Url::get('type',0);
			
			$config_arr		= array(	
									'status' 		=> (int)$status,
									'url_detail' 	=> $url_detail,
									'type' 			=> (int)$type
									);
			
			
			if(isset($_FILES['banner']) && $_FILES['banner']['error']==0)
			{
				$banner_file	=	ROOT_PATH.'promotion/popup';
				
				//$file_ext=strtolower(substr($_FILES['banner']['name'],strlen($_FILES['banner']['name'])-4,4));
				$file_ext	= AZLib::getExtension($_FILES['banner']['name']);
				
				if($type==0)//Ảnh
				{
					if(in_array($file_ext,array('.jpg','.gif')))
					{
						if(getimagesize($_FILES['banner']['tmp_name']))
						{	
							if(!move_uploaded_file( $_FILES['banner']['tmp_name'], $banner_file.'.gif'))
							{
								$this->setFormError('banner','Không up load được Banner!');
								return ;
							}
						}
						else 
						{
							$this->setFormError('banner','Sai định dạng ảnh!');
							return ;
						}
					}
					else 
					{
						$this->setFormError('banner','Chỉ chấp nhận các định dạng file .jpg , .gif !');
						return ;
					}
				}
				else 
				{
					if(in_array($file_ext,array('.swf')))
					{
						if(!move_uploaded_file( $_FILES['banner']['tmp_name'], $banner_file.$file_ext))
						{
							$this->setFormError('banner','Không up load được Banner!');
							return ;
						}
					}
					else 
					{
						$this->setFormError('banner','Flash đại diện chỉ chấp nhận các định dạng file .swf');
						return ;
					}
				}
			}
			
			$_SESSION['adv_cover_top'] = 0;
			
			if(isset(CGlobal::$configs['AdvCover']))
				DB::update('configs',array("conf_val"=>addslashes(serialize($config_arr))),"conf_key='AdvCover'");
			else
				DB::insert('configs',array("conf_key"=>'AdvCover',"conf_val"=>addslashes(serialize($config_arr))));
			
			AZLib::get_config(0,1);
		}
		
		if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']){
			$referer = $_SERVER['HTTP_REFERER'];
		}
		else{
			$referer = $_SERVER['REQUEST_URI'];
		}
		
		Url::redirect_url($referer);
	}
	
	function draw()
	{
		global $display;
		
		$config		=	@CGlobal::$configs['AdvCover']['conf_val'];
		
		$status		=	0;
		$url_detail	=	'';
		$type_		=	0;
		
		if($config	!='')
		{
			$config = unserialize(stripslashes($config));
			
			$status		=	(int)$config['status'];
			$type_		=	(int)$config['type'];
			
			$url_detail	=	$config['url_detail'];
		}
		
		if($type_==0)
			$type_='promotion';
		else 
			$type_='f_promotion';	
			
		$display->add('type_promotion',$type_);
				
		if(!isset($_SESSION['adv_cover_top']) || (isset($_SESSION['adv_cover_top']) && (int)$_SESSION['adv_cover_top']<1))
		{
			if($status)
			{
				if($url_detail!=''){
				
					$display->add('url_detail',$url_detail);
				}
				
				$_SESSION['adv_cover_top']=1;
			}
			
			$display->add('adv_on',$status);
		}
		else
		{
			$display->add('adv_on',0);
		}
		
		if(User::have_permit(ADMIN_ADV))
		{
			$display->add('beginform',$this->beginForm(true,'POST',false,false,true));
			$display->add('endform',$this->endForm(true));
			$display->add('url',$url_detail);
			$display->add('msg',$this->showFormErrorMessages(1));
			
			$type_arr=array(0=>'Ảnh',1=>'Flash');
			
			$type	=	(int)Url::get('type',0);
			$display->add('type_option',AZLib::getOption($type_arr,$type));  
			
			$status_arr=array(0=>'Tắt',1=>'Bật');
			$display->add('status_option',AZLib::getOption($status_arr,$status));
			
			$display->add('admin_url',Url::build('admin_promotion'));
		}
		
		$display->output("AdvCover");
	}
}
?>